package com.xysd.express.activity.freeride;

import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.xysd.express.R;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.eventbus.TypeWeightEvent;
import com.xysd.express.widget.MyDIYPopupWindow;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@ContentView(R.layout.activity_free_ride)
public class FreeRideActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.activity_free_ride)
    LinearLayout activity_free_ride;
    @ViewInject(R.id.car_small_ctv)
    CheckedTextView car_small_ctv;
    @ViewInject(R.id.car_middle_ctv)
    CheckedTextView car_middle_ctv;
    @ViewInject(R.id.car_large_ctv)
    CheckedTextView car_large_ctv;
    @ViewInject(R.id.send_layout)
    LinearLayout send_layout;
    @ViewInject(R.id.receive_layout)
    LinearLayout receive_layout;
    @ViewInject(R.id.goods_type_tv)
    TextView goods_type_tv;
    @ViewInject(R.id.time_tv)
    TextView time_tv;
    @ViewInject(R.id.price_tv)
    TextView price_tv;
    @ViewInject(R.id.commit_btn)
    Button commit_btn;

    @Override
    protected void init() {
        setTitleBar("顺货");
        car_small_ctv.setOnClickListener(this);
        car_middle_ctv.setOnClickListener(this);
        car_large_ctv.setOnClickListener(this);
        send_layout.setOnClickListener(this);
        receive_layout.setOnClickListener(this);
        goods_type_tv.setOnClickListener(this);
        time_tv.setOnClickListener(this);
        commit_btn.setOnClickListener(this);
    }

    private void setSelect(CheckedTextView ctv) {
        if (ctv.isChecked())
            return;
        car_small_ctv.setChecked(false);
        car_middle_ctv.setChecked(false);
        car_large_ctv.setChecked(false);
        ctv.setChecked(true);
    }

    //时间选择器
    private void showTimePicker(final TextView timeView) {
        TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String timeStr = sdf.format(date);
                timeView.setText(timeStr);
            }
        }).setTitleText("选择时间")//标题文字
                .isCyclic(false)//是否循环滚动
                .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                .setLabel("年", "月", "日", "时", "分", "秒")
                .build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }


    public int goodstype;
    public int weight;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(TypeWeightEvent event){
        goodstype=event.type;
        weight=event.weight;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.car_small_ctv:
                setSelect(car_small_ctv);
                break;
            case R.id.car_middle_ctv:
                setSelect(car_middle_ctv);
                break;
            case R.id.car_large_ctv:
                setSelect(car_large_ctv);
                break;
            case R.id.send_layout:

                break;
            case R.id.receive_layout:

                break;
            case R.id.goods_type_tv:
                new MyDIYPopupWindow(mActivity).showAtLocation(activity_free_ride, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.time_tv:
                showTimePicker(time_tv);
                break;
            case R.id.commit_btn:

                break;
        }

    }
}
